import requests
from bs4 import BeautifulSoup
import time
import os
import re

url = "https://rouman5.xyz/books/bf9aa35d-1a5d-4fcb-bcee-0e12fe8bb727"
resp = requests.get(url)
resp.encoding = 'utf-8'

# print(resp.text)
main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div", class_="bookid_chapterBox__CRrx9").find_all("a")
# alist = main_page.find("div", class_="bookid_chapter__20FJi").find_all("a") # 只爬第一张
for a in alist:
    href = 'https://rouman5.xyz' + a.get('href')  # 直接通过get就可以拿到属性的值
    # 拿到子页面的源代码
    child_page_resp = requests.get(href)
    child_page_resp.encoding = 'utf-8'
    child_page_text = child_page_resp.text
    # 从子页面中拿到图片的下载路径
    child_page = BeautifulSoup(child_page_text, "html.parser")
    mkname = re.sub("\D", "", child_page.title.string)  
    if len(mkname)==1:
        mkname = 'jiaoshou/00' + mkname
    elif len(mkname)==2:
        mkname = 'jiaoshou/0' + mkname
    if os.path.exists(mkname) is False:
        os.makedirs(mkname)

    img = child_page.find_all("img", class_="id_comicImage__2vwcn")
    for it in img:
        src = it.get("src")
        print(src)
        # 下载图片
        img_resp = requests.post(src)
        img_name = src.split("/")[-1]  # 拿到url中的最后一个/以后的内容
        with open(mkname + '/' +img_name, mode="wb") as f:
            f.write(img_resp.content)  # 图片内容写入文件

        print("over!!!", img_name)
        time.sleep(1)

print("all over!!!")